package com.doctor.ui.office;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.adapter.ShowImageAdapter;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.DialogHelper;
import com.doctor.comm.DialogHelper.NoticeDialogCallback;
import com.doctor.comm.FileHelper;
import com.doctor.comm.ImageHelper;
import com.doctor.comm.ImageHelper.TakePictureCallback;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseFragment;
import com.doctor.ui.DragImageActivity;
import com.doctor.ui.R;
import com.doctor.view.NoScrollGridView;

public class BaseInfoFrag extends BaseFragment{

	/**
	 * 发布信息和修改信息的父类
	 */
	
	protected Context context;
	protected EditText title,content;   //标题、内容
	protected TextView uploadBtn,takePhotoBtn;    //上传、拍照
	protected LinearLayout saveBtn,cancelBtn;     //保存、取消
	protected TextView dateBtn,timeBtn;    //日期、时间
	protected String table;  //要操作的数据库表
	protected Uri imageUri;
	protected Activity activity;
	//显示图片之用
	protected NoScrollGridView gridView;
	protected ShowImageAdapter adapter;
	protected List<String> beanList;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		context=getActivity();
		Bundle bundle=getArguments();
		table=bundle.getString(ConstConfig.TABLE);
		activity=getActivity();
	}
	
	//初始化控件
	@Override
	protected void initiaView(View view) {
		// TODO Auto-generated method stub
		Onclick onclick=new Onclick();
		
		gridView=(NoScrollGridView) view.findViewById(R.id.gridView);
		beanList=new ArrayList<String>();
		adapter=new ShowImageAdapter(context, beanList);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				String picPath=(String) arg1.getTag(R.id.bean_tag);
				Intent intent=new Intent(getActivity(),DragImageActivity.class);
				intent.putExtra(getResources().getString(R.string.uri), picPath);
				startActivity(intent);
				
			}
		});
		gridView.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, final View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				
				DialogHelper.noticeDialog(context, null, new NoticeDialogCallback() {	
					@Override
					public void onClicked(int position) {
						// TODO Auto-generated method stub
						if (position==1) {
							String pic=(String) arg1.getTag(R.id.bean_tag);
							adapter.removeBean(pic);
						}
					}
				});
				return true;
			}
		});
		
		title=(EditText) view.findViewById(R.id.title);
		content=(EditText) view.findViewById(R.id.content);
		uploadBtn=(TextView) view.findViewById(R.id.uploadBtn);
		uploadBtn.setOnClickListener(onclick);
		
		takePhotoBtn=(TextView) view.findViewById(R.id.takePhotoBtn);
		takePhotoBtn.setOnClickListener(onclick);
		
		saveBtn=(LinearLayout) view.findViewById(R.id.saveBtn);
		saveBtn.setOnClickListener(onclick);
		
		cancelBtn=(LinearLayout) view.findViewById(R.id.cancelBtn);
		cancelBtn.setOnClickListener(onclick);
		
		dateBtn=(TextView) view.findViewById(R.id.remind_date);
		dateBtn.setOnClickListener(onclick);
		
		timeBtn=(TextView) view.findViewById(R.id.remind_time);
		timeBtn.setOnClickListener(onclick);
	}
	
	protected final class Onclick implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v==cancelBtn) {
				getActivity().getSupportFragmentManager().popBackStack();
			} else if (v==dateBtn) {
				Calendar calendar=Calendar.getInstance();				
				DialogHelper.showDatePickerDialog(context, dateBtn, calendar, true);
			} else if (v==timeBtn) {
				Calendar calendar=Calendar.getInstance();
				DialogHelper.showTimePickerDialog(context, timeBtn, calendar, false);
			} else if (v==uploadBtn) {
				ImageHelper.selectPicture(BaseInfoFrag.this, ConstConfig.CHOOSE_PICTURE,null);
			} else if (v==takePhotoBtn) {
				ImageHelper.selectPicture(BaseInfoFrag.this, ConstConfig.TAKE_PICTURE,new TakePictureCallback() {					
					@Override
					public void onGetImageName(Uri imageUri) {
						// TODO Auto-generated method stub
						BaseInfoFrag.this.imageUri=imageUri;
					}
				});
			}
			else if (v==saveBtn) {
				String title_txt=title.getText().toString().trim();
				if (null==title_txt || "".equals(title_txt)) {
					Toast.makeText(context, "请填写标题！", Toast.LENGTH_SHORT).show();
					return;
				}
				
				String content_txt=content.getText().toString().trim();
				String date_txt=dateBtn.getText().toString();
				String time_txt=timeBtn.getText().toString();
				
				ContentValues addValues=new ContentValues();
				addValues.put("_title", title_txt);
				if (date_txt!=null && !"".equals(date_txt)) {
					addValues.put("_remind_date", date_txt);
					final String[] dateArray=date_txt.split("-");
					addValues.put("_date", dateArray[1]+"月"+dateArray[2]+"日");
				}
				if (time_txt!=null && !"".equals(time_txt)) {
					addValues.put("_remind_time", time_txt);
				}
				if (content_txt!=null && !"".equals(content_txt)) {
					addValues.put("_detail", content_txt);
				}
				if (adapter.getAll()!=null && !"".equals(adapter.getAll())) {
					addValues.put("_image", adapter.getAll());
				}
				addRecord(table, addValues);
				
				ContentValues modifyValues=new ContentValues();
				modifyValues.put("_title", title_txt);
				modifyValues.put("_remind_date", date_txt);
				if (date_txt!=null && !"".equals(date_txt)) {
					final String[] dateArray=date_txt.split("-");
					modifyValues.put("_date", dateArray[1]+"月"+dateArray[2]+"日");
				} else {
					modifyValues.put("_date", "");
				}
				modifyValues.put("_remind_time", time_txt);
				modifyValues.put("_detail", content_txt);
				modifyValues.put("_image", adapter.getAll());
				
				modifyRecord(table, modifyValues);
				
			}
		}
		
	}
	
	//添加操作,需要子类重写
	protected void addRecord(String tableName,ContentValues values){
		
	}
	
	//修改一条记录，需要子类重写
	protected void modifyRecord(String tableName,ContentValues values){
		
	}
	
	//选择图片或拍照完成后返回图片路径
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		//-1=RESULT_OK
		if (resultCode==-1) {
			Uri imageUri = null;	
			switch (requestCode) {
			case ConstConfig.CHOOSE_PICTURE:
				if (data!=null) {
					imageUri=data.getData();
				}
				break;
				
			case ConstConfig.TAKE_PICTURE:
				imageUri=BaseInfoFrag.this.imageUri;
				BaseInfoFrag.this.imageUri=null;
				break;
				
			default:
				break;
			}
			if (imageUri!=null) {
				String picPath = FileHelper.uri2Path(activity, imageUri);
				if (picPath !=null && picPath.length() > 0) {
					adapter.addBean(picPath);
					adapter.notifyDataSetChanged();
				}
			}
		} 
	}
	
	
	/**
	 * 根据表名确定闹钟的下一id
	 * @param table
	 */
	protected int getAlarmNextIdByTable(String table){
		int baseId = 0;
		if (ConstConfig.TODAY_EVENT_TABLE.equals(table)) {
			baseId=0;
		} else if (ConstConfig.SCHEDULE_TIME_TABLE.equals(table)) {
			baseId=ConstConfig.BASE_ID;
		} else if (ConstConfig.MEDICAL_INFO_TABLE.equals(table)) {
			baseId=2*ConstConfig.BASE_ID;
		} else if (ConstConfig.DRUGS_INFO_TABLE.equals(table)) {
			baseId=3*ConstConfig.BASE_ID;
		} else if (ConstConfig.IMMEDIATE_NOTES_TABLE.equals(table)) {
			baseId=4*ConstConfig.BASE_ID;
		} else if (ConstConfig.FILE_STORAGE_TABLE.equals(table)) {
			baseId=5*ConstConfig.BASE_ID;
		} else if (ConstConfig.ACCOUNT_BOOK_TABLE.equals(table)) {
			baseId=6*ConstConfig.BASE_ID;
		} else if (ConstConfig.BUY_DRUGS_TABLE.equals(table)) {
			baseId=7*ConstConfig.BASE_ID;
		} else if (ConstConfig.OTHER_TABLE.equals(table)) {
			baseId=8*ConstConfig.BASE_ID;
		}
		int maxId=DbOperator.getInstance(context).getAlarmMaxId(table);
		if (maxId<=0) {
			maxId=baseId+1;
		} else {
			maxId+=1;
		}
		return maxId;
	}
	
	
	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
		if (adapter != null) {
			adapter.clear();
		}
	}
	
}
